class VotesController < ApplicationController

  hobo_model_controller
  
  filter_parameter_logging "candidate_id"
  
  def index
    @voted_elections = current_user.get_voted_elections
    @unvoted_elections = current_user.get_unvoted_elections
  end
  
  def create
    if params[:vote][:candidate_id].nil?
      candidate = nil
    else
      candidate = Candidate.find(params[:vote][:candidate_id])
    end
    # TODO: validate hidden field and send hacker alert
    if request.post?
      election = Election.find(params[:vote][:election_id])
      
      # TODO: define exceptions
      if election.nil? or current_user.nil?
        raise Exception
      end
      if !election.active?
        raise Exception
      end
      
      @vote = current_user.cast_vote(election, candidate)
      Notifier.deliver_election_vote(current_user,@vote)
    end
  end
  
  def show_election
    election = Election.find(params[:id])
    
    if election.active?
      if current_user.didnt_vote(election)
        show_active_election(election)
      else
        render(:inline => '<panel class="blue"><p>The election is still active. You can only see the results when it\'s over.</p></panel>')
      end
    else
      if election.expired?
        show_election_results(election)
      else
        render(:inline => '<panel class="blue"><p>Election not yet started</p></panel>')
      end
    end
  end
  
  def show_active_election(election)
    @election = election
    render(:action => 'show_active_election', :layout => false) and return 
  end
  
  def show_election_results(election)
      @election = election
      @null_votes = election.null_votes.count
      @total_ballots = election.ballots.count
      @winner_candidate = Candidate.find(:first, :conditions => ["election_id = ? AND votes_count > ?", election.id, 0], :order => "votes_count DESC")
      @votes = election.votes
      @votes_list = votes_list(election)
      @no_shows = "%4.2f" % ((1-(@votes.count.to_f/@total_ballots.to_f))*100)
      @blank_ratio = "%4.2f" % ((@null_votes.to_f/@total_ballots.to_f)*100)
      
      render(:action => 'show_election_results', :layout => false) and return
  end
  
  # Returns the search results
  def search_uuid
    # If there is no params, return error message
    if params[:search].nil? or params[:election].nil?
      @results = "Error in search"
    end
    election_id = params[:election][:id].to_s
    uuid = params[:search].to_s
    # Finds the vote with the specified UUID
    tmp_vote = Vote.find(:first, :conditions => ["election_id = ? AND uuid = ?", election_id,uuid])
    # If there is no such vote, return not found
    if tmp_vote.nil?
      @results = "The EUUID: #{uuid} wasn't found."
    # If there is a vote matching that UUID
    else
      if tmp_vote.candidate.nil?
        candidate_name ="BLANK VOTE"
      else
        candidate_name = tmp_vote.candidate.name
      end
      @results = "The EUUID: #{uuid} voted for #{candidate_name}"
    end 

    # If there is an empty string in the search, return all the results
    # in a list form, setting @results => nil, so the view can render
    # the correct variable.
    if uuid.empty?
      election = Election.find_by_id(election_id)
      @results_list_form = votes_list(election) unless election.nil?
      @results = nil
    end
  end
  
  def votes_list(election)
    votes = Array.new
    election.votes.each do |vote|
    candidate = Candidate.find_by_id(vote.candidate_id)
      if candidate.nil?
        votes << [vote.uuid, "BLANK VOTE"]
      else
        votes << [vote.uuid, candidate.name]
      end
    end
    return votes
  end

end